//
//  MineFollowListViewController.m
//  ShowYork
//
//  Created by hxh on 2020/10/13.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "MineFollowListViewController.h"
#import "XHFollowSegmentControl.h"
#import "MineFollowListCell.h"
#import "MineFollowItemModel.h"
#import "MineFansListViewController.h"
#import "MineFollowedListViewController.h"


@interface MineFollowListViewController ()<UIScrollViewDelegate>
@property (nonatomic,strong)XHFollowSegmentControl *segmentControl;
@property (nonatomic,strong)UIScrollView *scrollView;

@property (nonatomic,strong)NSNumber *userId;
@property (nonatomic,copy)NSString *userName;

@property (nonatomic,assign)NSInteger currentIndex;



@end

@implementation MineFollowListViewController


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = YES;
        
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    blackLineImageView.hidden = NO;

}


- (instancetype)initWithUserInfoId:(NSNumber *)userId userName:(NSString *)userName index:(NSInteger)index{
    if (self = [super init]) {
        self.userId = userId;
        self.userName = userName;
        self.currentIndex = index;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isHideNavBar = [RPAppManager isShowNetworkView];
    
    if ([RPAppManager isShowNetworkView]) {
        [self setNetworkErrorUI];
    }else{
        [self setNetworkNormalUI];
    }
 
    // Do any additional setup after loading the view.
}

- (void)setNetworkNormalUI{
    [self setUI];
}
- (void)setNetworkErrorUI{
    @zf_weakify(self)
    [self showNetworkErrorWithNavTitle:self.userName completeBlock:^{
        [self setNetworkNormalUI];
    } cancelBlock:^{
        @zf_strongify(self)
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)setUI{
    
    [MobClick event:@"mine_focus_fans_show"];

    __weak typeof(self)weakSelf = self;
    self.segmentControl = [[XHFollowSegmentControl alloc]initWithSourceTitles:@[@"关注",@"粉丝"] completeBlock:^(NSInteger index) {
        [weakSelf.scrollView setContentOffset:CGPointMake(kScreenW * index, 0) animated:YES];
    }];
    [self.segmentControl showInView:self.view];
    
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.segmentControl.mas_bottom);
        make.left.right.bottom.equalTo(self.view);
    }];
    
    self.scrollView.contentSize = CGSizeMake(kScreenSize.width * 2, 0);
    
    
    MineFansListViewController *fanVC = [[MineFansListViewController alloc]initWithUserInfoId:self.userId];
    [self addChildViewController:fanVC];
    
    MineFollowedListViewController *followedVC = [[MineFollowedListViewController alloc]initWithUserInfoId:self.userId];
    [self addChildViewController:followedVC];
    
    [self.scrollView addSubview:fanVC.view];
    [self.scrollView addSubview:followedVC.view];
    
    [followedVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.equalTo(self.scrollView);
        make.leading.mas_equalTo(self.scrollView.mas_leading);
    }];
    
    [fanVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.equalTo(self.scrollView);
        make.leading.mas_equalTo(followedVC.view.mas_trailing);
    }];

    self.navigationItem.title = self.userName;
    
    [self.scrollView setContentOffset:CGPointMake(kScreenW * self.currentIndex, 0) animated:YES];

    self.segmentControl.index = self.currentIndex;
    
    fanVC.updateFansListCount = ^(NSNumber * _Nonnull fansCount) {
        weakSelf.segmentControl.fansCount = fansCount;
    };
    fanVC.addOrDeleteFollowListCount = ^(BOOL isAdd, MineFollowItemModel * _Nonnull currentModel) {
        NSInteger followCount = weakSelf.segmentControl.followCount.intValue;
        if (isAdd) {
            followCount++;
        }else{
            followCount--;
        }
        weakSelf.segmentControl.followCount = @(followCount);
    };

    followedVC.updateFollowedListCount = ^(NSNumber * _Nonnull followCount) {
        weakSelf.segmentControl.followCount = followCount;
    };
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.currentIndex = scrollView.contentOffset.x/kScreenW;
    self.segmentControl.index = self.currentIndex;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    self.currentIndex = scrollView.contentOffset.x/kScreenW;
    self.segmentControl.index = self.currentIndex;
}

- (UIImageView *)findHairlineImageViewUnder:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0)
    {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self findHairlineImageViewUnder:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

@end
